package frontend.parser.node;

import frontend.lexer.token.Token;

import java.util.ArrayList;

public abstract class Node {
    protected final ArrayList<Node> children = new ArrayList<>();
    public void add(Node node) {
        if (node != null) {
            children.add(node);
        }
    }
    public Token getFirstToken() {
        Node node = this;
        while (true) {
            if (node instanceof TokenNode tokenNode) {
                return tokenNode.getToken();
            }
            if (node.children.isEmpty()) {
                return null;
            }
            node = node.children.get(0);
        }
    }

    public Token getLastToken() {
        Node node = this;
        while (true) {
            if (node instanceof TokenNode tokenNode) {
                return tokenNode.getToken();
            }
            if (node.children.isEmpty()) {
                return null;
            }
            node = node.children.get(node.children.size() - 1);
        }
    }

    public ArrayList<Node> getChildren() {
        return children;
    }
}
